package com.winshare.saleAssistant.service.impl;

import com.winshare.saleAssistant.dao.SupplierMapper;
import com.winshare.saleAssistant.domain.dto.req.BaseIdDTO;
import com.winshare.saleAssistant.domain.dto.req.SupplierCreateDTO;
import com.winshare.saleAssistant.domain.dto.req.SupplierQueryDTO;
import com.winshare.saleAssistant.domain.dto.req.SupplierUpdateDTO;
import com.winshare.saleAssistant.domain.dto.resp.SupplierRespDTO;
import com.winshare.saleAssistant.domain.entity.SupplierDO;
import com.winshare.saleAssistant.domain.query.SupplierQuery;
import com.winshare.saleAssistant.exception.BusinessException;
import com.winshare.saleAssistant.mapper.SupplierMapStruct;
import com.winshare.saleAssistant.service.SupplierService;
import com.winshare.saleAssistant.util.Page;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

    private final SupplierMapper supplierMapper;

    public SupplierServiceImpl(SupplierMapper supplierMapper) {
        this.supplierMapper = supplierMapper;
    }

    @Override
    public void saveSupplier(SupplierCreateDTO supplierCreateDTO) {
        SupplierDO supplierDO = SupplierMapStruct.INSTANCE.fromSupplierCreateDTO(supplierCreateDTO);
        if(checkSupplierExist(supplierDO)) {
            throw new BusinessException("此供应商已存在");
        }
        supplierMapper.saveSupplier(supplierDO);
    }

    @Override
    public void updateSupplier(SupplierUpdateDTO supplierUpdateDTO) {
        SupplierDO supplierDO = SupplierMapStruct.INSTANCE.fromSupplierUpdateDTO(supplierUpdateDTO);
        if(checkSupplierExist(supplierDO)) {
            throw new BusinessException("此供应商已存在");
        }
        supplierMapper.updateSupplier(supplierDO);
    }

    @Override
    public SupplierRespDTO getSupplierDetail(BaseIdDTO baseIdDTO) {
        SupplierDO supplierDO = SupplierMapStruct.INSTANCE.fromBaseIdDTO(baseIdDTO);
        SupplierDO supplierDOInSql = supplierMapper.getSupplier(supplierDO);
        if(null == supplierDOInSql) {
            throw new BusinessException("此供应商已经不存在");
        }
        SupplierRespDTO supplierRespDTO = SupplierMapStruct.INSTANCE.fromSupplierDO(supplierDOInSql);
        return supplierRespDTO;
    }

    @Override
    public void deleteSupplier(BaseIdDTO baseIdDTO) {
        SupplierDO supplierDO = SupplierMapStruct.INSTANCE.fromBaseIdDTO(baseIdDTO);
        SupplierDO supplierDOInSql = supplierMapper.getSupplier(supplierDO);
        if(null == supplierDOInSql) {
            throw new BusinessException("此供应商已经不存在");
        }
        supplierMapper.deleteSupplier(supplierDO);
    }

    @Override
    public Page<SupplierRespDTO> getSupplierList(SupplierQueryDTO supplierQueryDTO) {
        SupplierQuery supplierQuery = SupplierMapStruct.INSTANCE.fromSupplierQueryDTO(supplierQueryDTO);
        supplierQuery.setOffsetIndex(supplierQueryDTO.getOffsetIndex());
        int totalCount = supplierMapper.querySupplierCount(supplierQuery);
        Integer pageIndex = supplierQueryDTO.getPageIndex();
        Integer pageSize = supplierQueryDTO.getPageSize();
        if(0 == totalCount) {
            return new Page<>(pageIndex,pageSize,totalCount,new ArrayList<>());
        }
        List<SupplierDO> supplierDOList = supplierMapper.findSupplierListByConditions(supplierQuery);
        List<SupplierRespDTO> supplierRespDTOList = SupplierMapStruct.INSTANCE.fromSupplierDOList(supplierDOList);
        return new Page<>(pageIndex,pageSize,totalCount,supplierRespDTOList);
    }

    private boolean checkSupplierExist(SupplierDO supplierDO) {
        return supplierMapper.countBySupplier(supplierDO) > 0;
    }
}
